/*
@hehung
2023-5-22
email: 1398660197@qq.com
wechat: hehung95
reproduced and please indicate the source @hehung
*/

#include "app_common.h"

#define LED_DEBUG
#undef LED_DEBUG

#ifdef LED_DEBUG
#include <stdio.h>
#endif


#if (LED_CTRL_TYPE == LED_CTRL_METHOD_IO)

#define LED_ON                     (BSP_IO_LEVEL_HIGH)
#define LED_OFF                    (BSP_IO_LEVEL_LOW)

static bsp_io_port_pin_t led_pin[LED_TOTAL_NUM] = 
{
	BSP_IO_PORT_04_PIN_00,    /* LED1 */
	BSP_IO_PORT_04_PIN_03,    /* LED2 */
	BSP_IO_PORT_04_PIN_04     /* LED3 */
};

#elif (LED_CTRL_TYPE == LED_CTRL_METHOD_PWM)

static uint16_t led1_duty_cycle = 10000;
static uint16_t led3_duty_cycle = 0;

#endif 

#if (LED_CTRL_TYPE == LED_CTRL_METHOD_IO)
void Led_Ctrl(uint8_t led_num, uint8_t led_level)
{
    (void)R_IOPORT_PinWrite(&g_ioport_ctrl, led_pin[led_num], (bsp_io_level_t)led_level);
}
#endif

void Led_Init(void)
{
	fsp_err_t err = FSP_SUCCESS;
	
	#if (LED_CTRL_TYPE == LED_CTRL_METHOD_PWM)
	/* Initialize Gpt6 */
	(void)R_GPT_Open(&g_timer6_ctrl, &g_timer6_cfg);
	/* Enable Gpt6 */
	(void)R_GPT_Enable(&g_timer6_ctrl);
	/* Start Gpt6 */
	(void)R_GPT_Start(&g_timer6_ctrl);
	
	/* Initialize Gpt3 */
	(void)R_GPT_Open(&g_timer3_ctrl, &g_timer3_cfg);
	/* Enable Gpt3 */
	(void)R_GPT_Enable(&g_timer3_ctrl);
	/* Start Gpt3 */
	(void)R_GPT_Start(&g_timer3_ctrl);
	
	/* setting the frequency */
	R_GPT_PeriodSet(&g_timer6_ctrl, 10000);  // frequency
	/* setting the frequency */
	R_GPT_PeriodSet(&g_timer3_ctrl, 10000);  // frequency

	err = R_GPT_DutyCycleSet(&g_timer6_ctrl, 10000, GPT_IO_PIN_GTIOCA);// duty cycle
    assert(FSP_SUCCESS == err);

	err = R_GPT_DutyCycleSet(&g_timer3_ctrl, 10000, GPT_IO_PIN_GTIOCA);// duty cycle
    assert(FSP_SUCCESS == err);

	err = R_GPT_DutyCycleSet(&g_timer3_ctrl, 10000, GPT_IO_PIN_GTIOCB);// duty cycle
    assert(FSP_SUCCESS == err);
	
	#endif
}

void Led_Running(void)
{
#if (LED_CTRL_TYPE == LED_CTRL_METHOD_IO)
	static uint8_t i = 0;
	static uint8_t flag = 0;

	Led_Ctrl(i, flag);
	
	i = (i < LED_TOTAL_NUM) ? (i+1) : (0);
	if (i == 0)
	{
		flag ^= 1;
	}
#elif (LED_CTRL_TYPE == LED_CTRL_METHOD_PWM)
	fsp_err_t err = FSP_SUCCESS;
	static uint16_t duty_cycle_flg = 0U;

	if (0U == duty_cycle_flg)
	{
		if (led3_duty_cycle < 10000)
		{
			led3_duty_cycle += 100;
		}
		else 
		{
			duty_cycle_flg = 1U;
		}
	}
	else
	{
		if (led3_duty_cycle > 0)
		{
			led3_duty_cycle -= 100;
		}
		else 
		{
			duty_cycle_flg = 0U;
		}		
	}

	// LED1
	err = R_GPT_DutyCycleSet(&g_timer6_ctrl, led1_duty_cycle, GPT_IO_PIN_GTIOCA);// duty cycle
    assert(FSP_SUCCESS == err);

//	err = R_GPT_DutyCycleSet(&g_timer3_ctrl, led3_duty_cycle, GPT_IO_PIN_GTIOCA);// duty cycle
//    assert(FSP_SUCCESS == err);

	// LED3
	err = R_GPT_DutyCycleSet(&g_timer3_ctrl, led3_duty_cycle, GPT_IO_PIN_GTIOCB);// duty cycle
    assert(FSP_SUCCESS == err);
#endif 
}


void Led_DutyCycleCtrl_Led1(uint8_t duty_cycle)
{
	led1_duty_cycle = (uint16_t)((100 - duty_cycle) * 100);
}

uint8_t Led_GetDutyCycle_Led1(void)
{
	return (uint8_t)(100 - (led1_duty_cycle / 100));
}
